/**
 * <p><b>Title:</b> PcImageSrcRulesController.java </p>
 * @Package com.wondertek.poms.image.controller
 */
package com.wondertek.poms.image.controller;

import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IPcImageSrcRulesService;
import com.wondertek.poms.dao.po.PcImageSrcRules;
import com.wondertek.poms.image.vo.PcImageSrcRulesVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p><b>PcImageSrcRulesController Description:</b> 原图名称表 Controller层实现</p>
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-19 09:35:09</p>
 */
@Api(value="PcImageSrcRulesController", description="原图名称 接口")
@RestController
@Scope("prototype")
@RequestMapping(value="/pcImageSrcRules")
public class PcImageSrcRulesController extends BaseController {
	
	@Autowired
	private IPcImageSrcRulesService pcImageSrcRulesService;
	
	@ApiOperation(value="原图名称 列表", notes="原图名称 列表")
	@GetMapping("/list")
	public ResultBean list(HttpServletRequest request,
			PcImageSrcRulesVo pcImageSrcRulesVo,
			@RequestParam(defaultValue="1") int page, 
			@RequestParam(defaultValue="20") int rows,
			@RequestParam(defaultValue="updatedTime") String sidx, 
			@RequestParam(defaultValue="desc") String sord) {
		PcImageSrcRules pcImageSrcRules = new PcImageSrcRules();
		BeanUtils.copyProperties(pcImageSrcRulesVo, pcImageSrcRules);
		
		Page<PcImageSrcRules> pages = pcImageSrcRulesService
				.findAll(pcImageSrcRules, page, rows, sord, sidx);
		
		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	@ApiOperation(value="原图名称 新增", notes="原图名称 新增")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "srcRuleName", required = true),
		@ApiImplicitParam(name = "srcRuleCode", required = true),
	})
	@PostMapping(value = "/add")
	@ResponseBody
	public ResultBean add(HttpServletRequest request, PcImageSrcRulesVo pcImageSrcRulesVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(pcImageSrcRulesVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		
		if (isRepeat(pcImageSrcRulesVo)) 
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
	
		PcImageSrcRules pcImageSrcRules = new PcImageSrcRules();
		BeanUtils.copyProperties(pcImageSrcRulesVo, pcImageSrcRules);
		
		PcImageSrcRules.putTimeAndUser(pcImageSrcRules);
		pcImageSrcRulesService.save(pcImageSrcRules);
		return ResultBean.ok(pcImageSrcRules);
	}
	
	/**
	 * <p><b>Title:</b> isRepeat</p>
	 * <p><b>Description:</b> 判断原图名称表 名称是否重复</p>
	 * @author douzi
	 * @param pcImageSrcRulesVo
	 */
	private boolean isRepeat(PcImageSrcRulesVo pcImageSrcRulesVo) {
		PcImageSrcRules temp = new PcImageSrcRules();
		temp.setSrcRuleCode(pcImageSrcRulesVo.getSrcRuleCode());
		Optional<PcImageSrcRules> pcImageSrcRulesOpt = pcImageSrcRulesService.findOne(Example.of(temp));
		if (pcImageSrcRulesOpt.isPresent()) {
			PcImageSrcRules pcImageSrcRules = pcImageSrcRulesOpt.get();
			if (pcImageSrcRulesVo.getId() != null && pcImageSrcRulesVo.getId() != pcImageSrcRules.getId())
				return true;
			else if (pcImageSrcRulesVo.getId() == null)
				return true;
		}
		return false;
	}
	
	@ApiOperation(value="原图名称 详情", notes="原图名称 详情")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@GetMapping(value = "/get")
	@ResponseBody
	public ResultBean get(Long id) {
		PcImageSrcRules pcImageSrcRules = pcImageSrcRulesService.get(id);
		return ResultBean.ok(pcImageSrcRules);
	}
	
	@ApiOperation(value="原图名称 变更", notes="原图名称 变更")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", required = true),
		@ApiImplicitParam(name = "srcRuleName", required = true),
		@ApiImplicitParam(name = "srcRuleCode", required = true),
	})
	@PutMapping(value = "/edit")
	@ResponseBody
	public ResultBean update(HttpServletRequest request, PcImageSrcRulesVo pcImageSrcRulesVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(pcImageSrcRulesVo, Constants.VALIDATE_UPDATE);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		
		if (isRepeat(pcImageSrcRulesVo)) {
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		}
		
		PcImageSrcRules pcImageSrcRules = new PcImageSrcRules();
		BeanUtils.copyProperties(pcImageSrcRulesVo, pcImageSrcRules);
		return ResultBean.ok(pcImageSrcRulesService.merge(pcImageSrcRules));
	}
	
	@ApiOperation(value="原图名称 删除", notes="原图名称 删除")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@DeleteMapping(value = "/del")
	@ResponseBody
	public ResultBean delete(Long id) {
		pcImageSrcRulesService.deleteLogic(id);
		return ResultBean.ok();
	}
}
